﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Reflection;

using HsTec.DigitalPlatform.Business;
using HsTec.DigitalPlatform.DataAccess;
using HsTec.DigitalPlatform.Model.DTO;


namespace HsTec.DigitalPlatform.WebSite.Controllers
{
    public class UserController : Controller
    {
        UserInfoManager userinfomanager = new UserInfoManager();
        DeptInfoManager deptinfomanager = new DeptInfoManager();
        RoleInfoManager roleinfomanager = new RoleInfoManager();
        //
        // GET: /User/

        public ActionResult Index()
        {
            UserModel usermodel = new UserModel();
            IQueryable<DeptInfo> deptinfolist = deptinfomanager.FindALLDeptInfo();
            List<SelectListItem> _deptinfoselect = new List<SelectListItem>();
            foreach (DeptInfo deptinfo in deptinfolist)
            {
                SelectListItem _deptselect = new SelectListItem();
                _deptselect.Text = deptinfo.deptname;
                _deptselect.Value = deptinfo.deptid.ToString();
                _deptinfoselect.Add(_deptselect);
            }
            usermodel.deptlist = _deptinfoselect;
            return View(usermodel);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult GetJsonGridData()
        {
            int page = int.Parse(Request.Form["page"]);
            int rp = int.Parse(Request.Form["rp"]);
            string sortname = Request.Form["sortname"].ToString();
            string sortorder = Request.Form["sortorder"].ToString();

            List<UserInfo> userinfos = userinfomanager.PageInfo(page, rp, sortname, sortorder).ToList<UserInfo>();
            JsonFlexiGridData jsonflexgriddata = new JsonFlexiGridData();
            jsonflexgriddata.page = page;
            jsonflexgriddata.total = userinfomanager.GetAllCount();
            if (jsonflexgriddata.rows == null)
            {
                jsonflexgriddata.rows = new List<FlexiGridRow>();
            }
            foreach (UserInfo userinfo in userinfos)
            {
                FlexiGridRow row = new FlexiGridRow()
                {
                    id = userinfo.userid.ToString(),
                    cell = GetPropertyList(userinfo)
                };
                jsonflexgriddata.rows.Add(row);
            }
            return Json(jsonflexgriddata);
        }
        private List<string> GetPropertyList(object obj)
        {
            List<string> propertyList = new List<string>();

            Type type = obj.GetType();
            PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo property in properties)
            {
                object o = property.GetValue(obj, null);
                propertyList.Add(o == null ? "" : o.ToString());
            }

            return propertyList;
        }

        [HttpGet]
        public ActionResult Add()
        {
            return PartialView();
        }
        [HttpPost]
        public ActionResult Add(FormCollection from)
        {
            UserInfo userinfo = new UserInfo();
            try
            {
                userinfo.username = from["username"];
                userinfo.useralias = from["useralias"];
                userinfo.password = "111111";
                userinfo.createtime = DateTime.Now;
                userinfo.deptid = int.Parse(from["deptid"]);
                userinfo.roleid = int.Parse(from["roleid"]);
                userinfomanager.Add(userinfo);
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("", "新增失败！");
            }
            return PartialView();
        }
        [HttpPost]
        public ActionResult Edit(FormCollection from)
        {
            try
            {
                UserInfo userinfo = userinfomanager.GetUserInfoByID(int.Parse(from["userid"]));
                userinfo.username = from["username"];
                userinfo.useralias = from["useralias"];
                userinfo.deptid = int.Parse(from["deptid"]);
                userinfo.roleid = int.Parse(from["roleid"]);
                userinfomanager.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("", "新增失败！");
            }
            return PartialView();
        }
        [HttpPost]
        public JsonResult Delete(string ids)
        {
            try
            {
                string[] ary = ids.Split(',');
                foreach (string id in ary)
                {
                    UserInfo userinfo = userinfomanager.GetUserInfoByID(int.Parse(id));
                    userinfomanager.Delete(userinfo);
                    return Json(new { msg = "删除成功！" });
                }
            }
            catch
            {
                ModelState.AddModelError("", "删除失败！");
            }
            return Json(new { msg = "删除失败！" });
        }
        [HttpGet]
        public ActionResult View(int id)
        {
            return PartialView();
        }

        public void Export()
        {
            List<UserInfo> userinfos = userinfomanager.FindALLUserInfo().ToList<UserInfo>();

            HttpContext context = System.Web.HttpContext.Current;
            context.Response.Clear();
            context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            context.Response.AppendHeader("Content-Disposition", "attachment;filename=111.xls");
            context.Response.Write("1111111111111111");
            context.Response.End(); 
        }
    }
}
